<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <div v-if="checkPermission(['roles@POST'])" class="filter-button">
        <router-link :to="{path:'/admin/role_add'}" class="route">
          <el-button
            v-permission="['roles@POST']"
            class="el-icon-edit"
            type="primary"
          >新增
          </el-button>
        </router-link>
      </div>
      <div class="filter-form">
        <el-form :model="filter" :inline="true" size="medium">
          <el-form-item label="">
            <el-input v-model="filter.keyword" placeholder="请输入角色名" />
          </el-form-item>
        </el-form>
        <div>
          <el-button v-permission="['roles@GET']" @click="search">
            搜索
          </el-button>
          <el-button
            v-permission="['roles@GET']"
            @click="resetForm"
          >重置
          </el-button>
        </div>
      </div>
    </div>
    <div v-loading="listLoading" class="content-container">
      <el-table
        :cell-style="{ textAlign: 'center' }"
        :data="list"
        border
        style="width: 100%"
        row-key="id"
        size="mini"
        :header-cell-class-name="headerStyle"
      >
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="status" label="权限状态">
          <template slot-scope="{ row }">
            <span v-if="row.status == 1" class="title-tag title-user-1 user-tag-1">正常</span>
            <span v-else class="title-tag title-user-0 user-tag-0">禁用</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="角色描述" />
        <el-table-column prop="created_at" label="创建时间">
          <template slot-scope="{ row }">
            <span>{{ row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPermission(['roles@PUT','roles@DELETE'])" label="操作" width="250px" fixed="right">
          <template slot-scope="{ row }">
            <router-link :to="{path:'/admin/role_edit', query:{id:row.id}}" class="route">
              <el-button
                v-permission="['roles@PUT']"
                type="text"
              >
                编辑
              </el-button>
            </router-link>
            <el-button
              v-if="row.id !==1"
              v-permission="['roles@DELETE']"
              type="text"
              class="button-red"
              @click="del(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="filter.page"
      :limit.sync="filter.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getRoles, delRole } from '@/api/role'
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  name: 'RolePermission',
  components: { Pagination },
  data() {
    return {
      total: 0,
      filter: {
        page: 1,
        limit: 10,
        keyword: ''
      },
      list: [],
      listLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    headerStyle({ row, rowIndex }) {
      return 'table-th'
    },
    getList() {
      this.listLoading = true
      getRoles(this.filter).then((response) => {
        const { list, total } = response.data
        this.list = list
        this.total = total
        this.listLoading = false
      })
    },
    search() {
      this.filter.page = 1
      this.getList()
    },
    resetForm() {
      this.filter.page = 1
      this.filter.keyword = ''
      this.getList()
    },
    del(item) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delRole(item.id).then(() => {
            this.$message.success('删除成功')
            this.getList()
          })
        })
    }
  }
}
</script>
